<?php
/**
 * 房间
 */
namespace app\socket;

use think\worker\Server;
use app\common\utils\RedisUtils;
use Workerman\Protocols\Websocket;
use app\service\Table;
use app\service\User;

Class Room extends Socket
{
    protected $socket = 'websocket://0.0.0.0:50001';

    // 枚举方法
    public $enumClassList = [
        'doClientProxy'         => [ 'args' => 3 ],
        'getClientProxyInfo'    => [ 'args' => 1 ],
        'heartBeat'             => [ 'args' => 0 ],
        'login'                 => [ 'args' => 1 ],
        'initTableInfo'         => [ 'args' => 1 ],
        'getConfig'             => [ 'args' => 2 ],
        'gameReadyOrNot'        => [ 'args' => 2 ],
        'gameStart'             => [ 'args' => 1 ],
        'sendLicensingToHand'   => [ 'args' => 1 ],
        'compareSize'           => [ 'args' => 1 ],
        'startCallCrads'        => [ 'args' => 2 ],
        'aCrads'                => [ 'args' => 2 ],
        'withNote'              => [ 'args' => 2 ],
        'filling'               => [ 'args' => 3 ],
        'disCrads'              => [ 'args' => 2 ],
        'allIn'                 => [ 'args' => 2 ],
        'exitRoom'              => [ 'args' => 2 ]
    ];

    // 链接uid
    protected $connid;

    public function __construct()
    {
        parent::__construct();
        $this->connid = [];
    }

    // 客户端连接
    public function onConnect($conn)
    {
        $conn->websocketType = Websocket::BINARY_TYPE_ARRAYBUFFER;
        echo "客户加入牌桌id: ".$conn->id."\n";
        $this->sendDescribe($conn);
    }

    // 客户端关闭连接
    public function onClose($conn)
    {
        echo "客户离开牌桌id: ".$conn->id."\n";
    }

    /**
     * 登录之后解析
     *
     * @param  [type] $args [description]
     * @return [type]       [description]
     */
    public function login($args)
    {
        if (empty($args) || empty($args[0])) {
            return $this->wsFailed('游戏登录认证失败');
        }

        $uinfo = solveJwt($args[0]);
        if (empty($uinfo['id'])) {
            return $this->wsFailed('登录失败，数据解析错误');
        }

        // 获取信息
        $u_model = (new User())->u_model;
        $data = $u_model->where(['uid' => $uinfo['id']])->find();

        // 判断用户是否存在游戏服务中
        $ugame = RedisUtils::get(REDIS_USER_TABLE.$uinfo['id']);
        if (empty($ugame)) {
            $data['isInGame'] = false;
            return $this->wsSuccess($data, '连接失败，您不在该房间中');
        }
        $data['isInGame'] = true;
        $data['table_info'] = $ugame;

        return $this->wsSuccess($data);
    }

    /**
     * 初始化牌桌
     *
     * @param  [type] $args 0=>tid
     * @return [type]       [description]
     */
    public function initTableInfo($args)
    {
        if (empty($args[0])) {
            return $this->wsFailed('初始化牌桌失败1');
        }

        if ((new Table)->initTable($args[0])) {
            return $this->wsSuccess('初始化牌桌成功');
        } else {
            return $this->wsFailed('初始化牌桌失败2');
        }
    }

    /**
     * 游戏准备/取消准备
     *
     * @param  [type] $args 0 => token 1 => tid
     * @return [type]       [description]
     */
    public function gameReadyOrNot($args)
    {
        $getId = $this->tokenGetId($args[0]);
        if (empty($getId['uid'])) {
            return $getId;
        }

        if ((new Table)->gameReadyOrNotReady($getId['uid'], $args[1])) {
            return $this->wsSuccess('更新成功');
        } else {
            return $this->wsFailed('游戏已经开始或游戏不存在');
        }
    }

    /**
     * 开始游戏
     *
     * @param  [type] $args tid
     * @return [type]       [description]
     */
    public function gameStart($args)
    {
        if (empty($args[0])) {
            return $this->wsFailed('开始游戏失败，配置错误');
        }

        return $this->wsResult((new Table)->gameStart($args[0]));
    }

    /**
     * 发手牌
     *
     * @param  [type] $args 0 => $tid
     * @return [type]       [description]
     */
    public function sendLicensingToHand($args)
    {
        if ((new Table)->sendLicensingToHand($args[0])) {
            return $this->wsSuccess('手牌发放成功');
        } else {
            return $this->wsFailed('手牌发放失败');
        }
    }

    /**
     * 获取牌桌配置
     *
     * @param  [type] $args 0 => token 1 => $tid
     * @return [type]       [description]
     */
    public function getConfig($args)
    {
        $getId = $this->tokenGetId($args[0]);
        if (empty($getId['uid'])) {
            return $getId;
        }

        $key = REDIS_TABLE_JOIN.$args[1];
        $tableRedis = RedisUtils::get($key);
        if (empty($tableRedis)) {
            return $this->wsFailed('获取配置失败');
        }

        (new Table)->reStartGame($args[1]);
        $tableRedis['selfid'] = $getId['uid'];
        $tableRedis['tid'] = $args[1];
        return $this->wsSuccess($tableRedis);
    }

    /**
     * 退出房间
     *
     * @param  [type] $args 0 => tid, 1 => token
     * @return [type]       [description]
     */
    public function exitRoom($args)
    {
        $getId = $this->tokenGetId($args[1]);
        if (empty($getId['uid'])) {
            return $getId;
        }

        if ((new Table)->leaveTablePos($getId['uid'], $args[0])) {
            return $this->wsSuccess([], '退出房间成功');
        } else {
            return $this->wsFailed('退出房间失败');
        }
    }

    /**
     * 比较牌面大小
     *
     * @param  [type] $args tid
     * @return [type]       [description]
     */
    public function compareSize($args)
    {
        if ((new Table)->compareShowSize($args[0])) {
            return $this->wsSuccess('大小比较成功');
        } else {
            return $this->wsFailed('大小比较失败');
        }
    }

    /**
     * 开始叫牌
     *
     * @param  [type] $args 0=>tid 1=>data
     * @return [type]       [description]
     */
    public function startCallCrads($args)
    {
        $data = (new Table)->startCallCrads($args[0], $args[1]);
        // (new Table)->sendCard($args[0]);
        return $this->wsSuccess($data);
    }

    /**
     * 过牌
     *
     * @param  [type] $args 0=>tid 1=>uid
     * @return [type]       [description]
     */
    public function aCrads($args)
    {
        $getId = $this->tokenGetId($args[1]);
        if (empty($getId['uid'])) {
            return $getId;
        }

        $status = (new Table)->aCrads($args[0], $getId['uid']);
        if ($status == 200) {
            return $this->wsSuccess([], '过牌成功');
        } elseif ($status == 1) {
            return $this->wsFailed('过牌失败');
        } elseif ($status == 2) {
            return $this->wsFailed('还没到您叫牌');
        } elseif ($status == 3) {
            return $this->wsFailed('您当前不能过牌');
        }
    }

    /**
     * 跟牌
     *
     * @param  [type] $args 0=>tid 1=>uid
     * @return [type]       [description]
     */
    public function withNote($args)
    {
        $getId = $this->tokenGetId($args[1]);
        if (empty($getId['uid'])) {
            return $getId;
        }

        $status = (new Table)->withNote($args[0], $getId['uid']);
        if ($status) {
            return $this->wsSuccess([], '跟注成功');
        } else {
            return $this->wsFailed('跟注失败');
        }
    }

    /**
     * 加注
     *
     * @param  [type] $args 0=>tid 1=>uid 2=>*?
     * @return [type]       [description]
     */
    public function filling($args)
    {
        $getId = $this->tokenGetId($args[1]);
        if (empty($getId['uid'])) {
            return $getId;
        }

        $status = (new Table)->filling($args[0], $getId['uid'], $args[2]);
        if ($status == 200) {
            return $this->wsSuccess([], '加注成功');
        } elseif ($status == 1) {
            return $this->wsFailed('余额不足');
        } elseif ($status == 2) {
            return $this->wsFailed('还没到您叫牌');
        } elseif ($status == 3) {
            return $this->wsFailed('您当前不能过牌');
        }

        return $this->wsFailed('加注错误');
    }

    /**
     * 弃牌
     *
     * @param  [type] $args 0=>tid 1=>uid
     * @return [type]       [description]
     */
    public function disCrads($args)
    {
        $getId = $this->tokenGetId($args[1]);
        if (empty($getId['uid'])) {
            return $getId;
        }

        $status = (new Table)->disCrads($args[0], $getId['uid']);
        if ($status) {
            return $this->wsSuccess([], '弃牌成功');
        } else {
            return $this->wsFailed('弃牌失败');
        }
    }

    /**
     * 梭哈
     *
     * @param  [type] $args 0=>tid 1=>uid
     * @return [type]       [description]
     */
    public function allIn($args)
    {
        $getId = $this->tokenGetId($args[1]);
        if (empty($getId['uid'])) {
            return $getId;
        }

        $status = (new Table)->allIn($args[0], $getId['uid']);
        if ($status) {
            return $this->wsSuccess([], '梭哈成功');
        } else {
            return $this->wsFailed('梭哈失败');
        }
    }
}
